<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Zhidu;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ZhiduController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Zhidu(), function (Grid $grid) {

            $grid->column('title');
            $grid->column('leixing');

            $grid->column('signdate', '修订日期')->display(function ($signdate) {
                return  date('Y-m', strtotime($signdate));
            });

            // $grid->column('zhidufiles');
            // $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Zhidu(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('leixing');
            $show->field('signdate');
            $show->zhidufiles()->file();
            // $show->field('created_at');
            // $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Zhidu(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            // $form->text('leixing');治安
            $form->radio('leixing')->options(['安全生产' => '安全生产', '消防' => '消防', '治安' => '治安', '其他' => '其他'])->default("安全生产")->required();
            $form->date('signdate');
            // $form->text('zhidufiles');
            $form->multipleFile('zhidufiles')->limit(3)->autoUpload()->saving(function ($files) {
                return json_encode($files);
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
